<template>
  <div id="app">
    <router-view />
    <audio ref="audio" muted src="/order.mp3"  hidden></audio>
  </div>
</template>

<script>
import api from '@/api'
export default {
  name: 'App',
  data() {
    return {
      count: 0
    }
  },
  methods: {
    message() {
      this.$notify({
        title: `您有${this.count}条新订单`,
        message: `点击查看`,
        dangerouslyUseHTMLString: true,
        onClick: () => {
          const url = this.$route.path
          if (url === '/order/list') {
            window.location.reload()
          } else {
            this.$router.push('/order/list')
          }
        },
        type: 'success',
        duration: 3000
      })
      const player = this.$refs.audio
      if (player) {
        player.play()
      }
    }
  },
  mounted() {
    setInterval(() => {
      const url = this.$route.path
      // console.log(url)
      if (url !== '/order/list' || 1 === 1) {
        api.supplier.hasNew().then(response => {
          const { data } = response
          const { count } = data
          console.log(count)
          this.count = count
          if (count > 0) {
            this.message()
          }
        })
      }
    }, 1000 * 60 * 5)
  }
}
</script>
